package com.test.cities.service.impl;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.cities.dto.BookDTO;
import com.test.cities.mapper.BookTestMapper;
import com.test.cities.po.Book;
import com.test.cities.service.BookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


@Service
public class BookServiceImpl implements BookService {
	private static final Logger logger = LoggerFactory.getLogger(BookServiceImpl.class);

	@Autowired
	private BookTestMapper bookTestMapper;

	@Override
	public List<Book> search(BookDTO bookDTO) {
		logger.debug("ServiceImpl查询"+bookDTO.toString());

		QueryWrapper<Book> queryWrapper = new QueryWrapper<Book>();

		if(bookDTO.getCode() != null && !bookDTO.getCode().isEmpty()) {
			queryWrapper.eq("code", bookDTO.getCode());
		}
		if(bookDTO.getAuthor() != null && !bookDTO.getAuthor().isEmpty()) {
			queryWrapper.like("author", bookDTO.getAuthor());
		}
		if(bookDTO.getName() != null && !bookDTO.getName().isEmpty()) {
			queryWrapper.like("name", bookDTO.getName());
		}
		if(bookDTO.getStartTime() != null) {
			queryWrapper.ge("publishDate", bookDTO.getStartTime());
		}
		if(bookDTO.getEndTime() != null) {
			queryWrapper.le("publishDate", bookDTO.getEndTime());
		}
		queryWrapper.orderByDesc("createTime");
		List<Book> resultList = bookTestMapper.selectList(queryWrapper);
		logger.debug("ServiceImpl查询结果"+resultList.toString());
		return resultList;
	}

	@Override
	public void insert(Book book) {
		logger.debug("ServiceImpl保存"+book.toString());
		book.setCreateTime(LocalDate.now());
		int status = bookTestMapper.insert(book);
		logger.debug("保存成功:" + status);
	}

	@Override
	public List<Book> searchAll() {
		QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("createTime");
		List<Book> resultList = bookTestMapper.selectList(queryWrapper);
		logger.debug("ServiceImpl查询结果"+resultList.toString());

		return resultList;
	}

	@Override
	public PageInfo<Book> searchAllPage(Integer pageIndex, Integer pageSize) {
		PageHelper.startPage(pageIndex,pageSize);
		QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("createTime");
		List<Book> bookList = bookTestMapper.selectList(queryWrapper);
		PageInfo<Book> pageInfo = new PageInfo<Book>(bookList);

		return pageInfo;
	}

	@Override
	public PageInfo<Book> searchByDto(Integer pageIndex, Integer pageSize, BookDTO bookDTO) {
		PageHelper.startPage(pageIndex,pageSize);
		logger.debug("ServiceImpl查询"+bookDTO.toString());

		QueryWrapper<Book> queryWrapper = new QueryWrapper<Book>();

		if(bookDTO.getCode() != null && !bookDTO.getCode().isEmpty()) {
			queryWrapper.eq("code", bookDTO.getCode());
		}
		if(bookDTO.getAuthor() != null && !bookDTO.getAuthor().isEmpty()) {
			queryWrapper.like("author", bookDTO.getAuthor());
		}
		if(bookDTO.getName() != null && !bookDTO.getName().isEmpty()) {
			queryWrapper.like("name", bookDTO.getName());
		}
		if(bookDTO.getStartTime() != null) {
			queryWrapper.ge("publishDate", bookDTO.getStartTime());
		}
		if(bookDTO.getEndTime() != null) {
			queryWrapper.le("publishDate", bookDTO.getEndTime());
		}
		queryWrapper.orderByDesc("createTime");
		List<Book> bookList = bookTestMapper.selectList(queryWrapper);
		PageInfo<Book> pageInfo = new PageInfo<Book>(bookList);

		return pageInfo;
	}

	@Override
	public Book searchByCode(String code) {
		logger.debug("ServiceImpl查询:"+code);
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("code", code);
		Book resultBook = bookTestMapper.selectOne(queryWrapper);
		return resultBook;
	}

	@Override
	public void edit(Book book) {
		logger.debug("ServiceImpl更新"+book.toString());
		book.setUpdateTime(LocalDate.now());
		QueryWrapper<Book> queryWrapper = new QueryWrapper<Book>();
		queryWrapper.eq("code", book.getCode());
		int status = bookTestMapper.update(book, queryWrapper);
		logger.debug("修改成功:" + status);
	}
}